package com.zr.task.dialog;

import android.annotation.SuppressLint;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;

import com.google.gson.Gson;
import com.zh.baselibrary.utils.DisplayUtil;
import com.zh.baselibrary.utils.IntentUtils;
import com.zh.model.home.QrCodeBean;
import com.zr.task.R;
import com.zr.task.activity.AboutActivity;
import com.zr.task.activity.ConfigSetActivity;
import com.zr.task.utils.DeviceUtil;
import com.zr.task.utils.ZXingUtils;

import butterknife.BindView;
import butterknife.ButterKnife;

public class QrDialog extends Dialog {
    @BindView(R.id.iv_qr_code)
    ImageView mIvQrCode;
    @BindView(R.id.pb)
    ProgressBar mPb;
    public QrDialog(@NonNull Context context,int themeResId) {
        super(context,themeResId);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_qr);
        ButterKnife.bind(this);
        initDatas();
        mIvQrCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                threeClick();
            }
        });

    }
    private void initDatas() {
        mIvQrCode.setVisibility(View.GONE);
        Point mPoint = DisplayUtil.getScreenMetrics(getContext());
        int wight = (mPoint.x / 5) * 4;
        QrCodeBean mBean = DeviceUtil.getDeviceBean(getContext());

        new Thread(new Runnable() {
            @Override
            public void run() {
                Gson mGson = new Gson();
                String content = mGson.toJson(mBean);
                Bitmap bmQR = ZXingUtils.createQRImage(content, wight, wight);
                Bitmap bmLogo = BitmapFactory.decodeResource(getContext().getResources(), R.drawable.logo);
                Bitmap bitmap = ZXingUtils.addLogo(bmQR, bmLogo);
                Message msg = new Message();
                msg.obj = bitmap;
                mHandler.sendMessage(msg);
            }
        }).start();
    }
    @SuppressLint("HandlerLeak")
    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            mPb.setVisibility(View.GONE);
            mIvQrCode.setVisibility(View.VISIBLE);
            Bitmap bitmap = (Bitmap) msg.obj;
            mIvQrCode.setImageBitmap(bitmap);
        }
    };
    int count;

    Long start, end;

    private void threeClick() {
        count++;
        if (count == 1) {
            start = System.currentTimeMillis();
        }
        if (count == 3) {
            end = System.currentTimeMillis();
        }
        if (count >= 3) {
            if ((end - start) < 700) {
                IntentUtils.instance().startActivity(getContext(), ConfigSetActivity.class);
            }
            count = 0;
        }
        if ((System.currentTimeMillis() - start) > 1000) {
            count = 0;
            start = System.currentTimeMillis();
        }
    }
}
